/** 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.free.i.virtual.machine.hal.devicealias;

import it.free.i.virtual.machine.hal.devicealias.command.DeviceAliasPortMessage;
import it.free.i.virtual.machine.hal.devicealias.echo.CollisionEcho;
import it.free.i.virtual.machine.hal.devicealias.echo.PingEcho;
import it.free.i.virtual.machine.hal.devicealias.echo.PositionEcho;
import it.free.i.virtual.machine.hal.devicealias.echo.DynamicParameterChangingEcho;

import javax.management.Notification;

/**
 * Facilities to handle notofications in various ways
 * @author nein
 */
public final class NotificationFacilities {

	private NotificationFacilities(){}
	
	/**
	 * Render to string the notificatin received
	 * @param notification is the notification to represent as a string
	 * @param receivedNotificationNumber is the number of the internal counter
	 * @return a string with data taked from the notification
	 */
	public static final String notificationToString(Notification notification, double receivedNotificationNumber){
		
		StringBuilder stringBuilder = new StringBuilder();
		
		
		stringBuilder.append(System.getProperty("line.separator"));
		stringBuilder.append("****************************");
		stringBuilder.append(System.getProperty("line.separator"));
		stringBuilder.append(receivedNotificationNumber);
		stringBuilder.append(System.getProperty("line.separator"));
		stringBuilder.append("****************************");
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("Sequence number: ");
		stringBuilder.append(notification.getSequenceNumber());
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("Source: ");
		stringBuilder.append(notification.getSource());
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("Timestamp: ");
		stringBuilder.append(notification.getTimeStamp());
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("User data: ");
		DeviceAliasPortMessage deviceAliasPortMessage = (DeviceAliasPortMessage) notification.getUserData();
		stringBuilder.append(System.getProperty("line.separator"));
		
		Object payload = deviceAliasPortMessage.getPayload();
		
		stringBuilder.append("User data payload: ");
		stringBuilder.append(payload);
		stringBuilder.append(System.getProperty("line.separator"));
		
		if(payload instanceof PositionEcho) {
		   stringBuilder.append(payloadToString((PositionEcho)payload));   		
		}else if(payload instanceof CollisionEcho) {
		         stringBuilder.append(payloadToString((CollisionEcho)payload));   		
		}else if(payload instanceof PingEcho) {
		         stringBuilder.append(payloadToString((PingEcho)payload));   		
		}else if(payload instanceof DynamicParameterChangingEcho){
				 stringBuilder.append(payloadToString((DynamicParameterChangingEcho)payload));
		}else{
			  //Levels
			  stringBuilder.append("Renderer not available for type: " + payload.getClass().getName());
		}
		
		stringBuilder.append("User data payload data type: ");
		stringBuilder.append(deviceAliasPortMessage.getPayloadDataType());
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("Class: ");
		stringBuilder.append(notification.getClass());
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("Message: ");
		stringBuilder.append(notification.getMessage());
		stringBuilder.append(System.getProperty("line.separator"));
		
		stringBuilder.append("Type: ");
		stringBuilder.append(notification.getType());
		stringBuilder.append(System.getProperty("line.separator"));
		stringBuilder.append("****************************");
		
		
		return stringBuilder.toString();
	}
	
	/**
	 * Represent as a string the content of a payload
	 * @param positionEcho is the input
	 * @return the string for the passed object
	 */
	public static final String payloadToString(PositionEcho positionEcho){
		
		StringBuilder renderer = new StringBuilder();
		
		renderer.append("Position echo center of mass position: ");
		renderer.append(positionEcho.getCenterOfMassPosition().toString());
		renderer.append(System.getProperty("line.separator"));
		
		renderer.append("Position echo linear velocity: ");
		renderer.append(positionEcho.getLinearVelocity());
		renderer.append(System.getProperty("line.separator"));
		
		return renderer.toString();
		
	}
	
	/**
	 * Represent as a string the content of a payload
	 * @param collisionEcho is the input
	 * @return the string for the passed object
	 */
	public static final String payloadToString(CollisionEcho collisionEcho){
		
		StringBuilder renderer = new StringBuilder();
		
		renderer.append("Collision echo impacted object type: ");
		renderer.append(collisionEcho.getImpactedObjectType());
		renderer.append(System.getProperty("line.separator"));
		
		renderer.append("Collision echo center of mass position: ");
		renderer.append(collisionEcho.getCenterOfMassPosition());
		renderer.append(System.getProperty("line.separator"));
		
		renderer.append("Collision echo impacted object description: ");
		renderer.append(collisionEcho.getImpactedObjectDescription());
		renderer.append(System.getProperty("line.separator"));
		
		return renderer.toString();
	}
	
	/**
	 * Represent as a string the content of a payload
	 * @param pingEcho is the input
	 * @return the string for the passed object
	 */
	public static final String payloadToString(PingEcho pingEcho){
		
		StringBuilder renderer = new StringBuilder();
		
		renderer.append("Ping echo value: ");
		renderer.append(pingEcho.getEchoValue());
		renderer.append(System.getProperty("line.separator"));
		
		return renderer.toString();
	}
	
	/**
	 * Represent as a string the content of a payload
	 * @param dynamicParameterChangingEcho is the input
	 * @return the string for the passed object
	 */
	public static final String payloadToString(DynamicParameterChangingEcho dynamicParameterChangingEcho){
		
		StringBuilder renderer = new StringBuilder();
		
		renderer.append("Dynamic parameter changing echo parameter name: ");
		renderer.append(dynamicParameterChangingEcho.getDynamicParameterName());
		renderer.append(System.getProperty("line.separator"));
		
		renderer.append("Dynamic parameter changing echo parameter level: ");
		renderer.append(dynamicParameterChangingEcho.getLevel().toString());
		renderer.append(System.getProperty("line.separator"));
		
		return renderer.toString();
	}
	
}
